package com.xbongbong.paas.service.workflow.setting.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.DateEnum;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.workflow.settings.WorkflowGetPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.util.WorkflowNodeUtil;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.enums.workflow.WorkflowValueTypeEnum;
import com.xbongbong.workflow.pojo.nodes.WorkflowConditionPojo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/01 20:05
 */
@Service("conditionNodeDateTimeServiceImpl")
public class ConditionNodeDateTimeServiceImpl extends AbstractConditionNodeFieldService {
    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.DATETIME.getType(), FieldTypeEnum.ADDTIME.getType(), FieldTypeEnum.UPDATETIME.getType());
    }


    @Override
    public String conditionField(WorkflowConditionPojo workflowConditionPojo, WorkflowGetPojo workflowGetPojo) throws XbbException {
        String value = "";
        Map<String, FieldAttrEntity> explainMap = WorkflowNodeUtil.getExplainMap(workflowGetPojo, workflowConditionPojo.getBusinessType(), workflowConditionPojo.getFormId());
        String attrName = workflowConditionPojo.getName();
        if (explainMap.containsKey(workflowConditionPojo.getAttr())) {
            attrName = explainMap.get(workflowConditionPojo.getAttr()).getAttrName();
        } else if (Objects.equals(workflowConditionPojo.getAttr(), FieldTypeEnum.ADDTIME.getAlias())) {
            attrName = I18nMessageUtil.getMessage(CommonConstant.CREATE_TIME);
        } else if (Objects.equals(workflowConditionPojo.getAttr(), FieldTypeEnum.UPDATETIME.getAlias())) {
            attrName = I18nMessageUtil.getMessage(CommonConstant.UPDATE_TIME);
        } else if (Objects.equals(workflowConditionPojo.getAttr(), FieldTypeEnum.CREATORID.getAlias())) {
            attrName = I18nMessageUtil.getMessage(CommonConstant.CREATOR);
        }
        String symbol = ConditionEnum.getMemoBySymbo(workflowConditionPojo.getSymbol());
        if (Objects.equals(WorkflowValueTypeEnum.FIXED_VALUE.getType(), workflowConditionPojo.getValueType())) {
            if (CollectionUtils.isNotEmpty(workflowConditionPojo.getValue())) {
                List<String> valueList = new ArrayList<>();
                String format = DateTimeEnum.TIME.getType();
                if (explainMap.containsKey(workflowConditionPojo.getAttr())) {
                    FieldAttrEntity fieldAttrEntity = explainMap.get(workflowConditionPojo.getAttr());
                    format = fieldAttrEntity.getDateType();
                }
                for (Object item : workflowConditionPojo.getValue()) {
                    if (Objects.equals(workflowConditionPojo.getSymbol(), ConditionEnum.DYNAMIC.getSymbol())) {
                        String value4Show = DateEnum.getDateEnumMemo(item.toString());
                        valueList.add(value4Show);
                    } else {
                        String date = DateTimeUtil.getStringEpochSecond(Long.valueOf(item.toString()), format);
                        valueList.add(date);
                    }
                }
                if (Objects.equals(ConditionEnum.RANGE.getSymbol(), workflowConditionPojo.getSymbol())
                        || Objects.equals(ConditionEnum.NORANGE.getSymbol(), workflowConditionPojo.getSymbol())) {
                    value = String.format(attrName + symbol) + StringUtils.join(valueList, "~");
                } else {
                    value = attrName + symbol + StringUtils.join(valueList, "~");
                }
            }else {
                // 为空、不为空
                value = attrName + symbol;
            }
        } else {
            value = getDynamicValue(attrName, symbol, workflowGetPojo, workflowConditionPojo);
        }
        return value;
    }
}
